package net.apps123.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.apps123.commonaclasses.MyBaseClass;
import net.apps123.commonaclasses.SpectrumView;
import net.apps123.commonaclasses.Xaxis;
import net.apps123.commonaclasses.Yaxis;
import net.apps123.soundmeter.R;

public class SplView extends RelativeLayout {


    private Context mContext;

    protected SpectrumView spectrumView3;
    protected SpectrumView spectrumView1;
    protected SpectrumView fftSpectrumView;

    protected Yaxis yAxis3;
    protected Yaxis yAxis1;
    protected Yaxis yAxisFft;

    protected Xaxis xAxis3;
    protected Xaxis xAxis1;
    protected Xaxis xAxisFft;

    private TextView ipTv;
    private TextView spTv;
    private Button swapBtn;
    MyBaseClass.Selection selection = MyBaseClass.Selection.ThirdOctave;


    MyBaseClass.DisplayData Outputs = new MyBaseClass.DisplayData();

    //设置1/3倍频程个数
    int noSpectralLines3 = 30;
    //设置倍频程个数
    int noSpectralLines1 = 10;
    //设置fft个数
    int noFftSpectralLines = 512;
//        noFftSpectralLines = 1024;


    public SplView(Context context) {
        super(context);
        mContext = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.spl_layout, this);
        InitViews( mContext);
    }

    public SplView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.spl_layout, this);
        InitViews( mContext);

    }

    public SplView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.spl_layout, this);
        InitViews( mContext);
    }


    //设置标题文字的方法
    private void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
//            tv_title.setText(title);
        }
    }

    //对左边按钮设置事件的方法
    private void setLeftListener(OnClickListener onClickListener) {
//        iv_back.setOnClickListener(onClickListener);
    }


    public void InitViews(Context mContext) {
        this.mContext = mContext;
        ipTv = findViewById(R.id.ipTv);
        spTv = findViewById(R.id.spTv);
        swapBtn = findViewById(R.id.swapBtn);
        swapBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selection == MyBaseClass.Selection.ThirdOctave){
                    selection = onSwap(MyBaseClass.Selection.Octave, swapBtn);
                }else if (selection == MyBaseClass.Selection.Octave){
                    selection = onSwap(MyBaseClass.Selection.Fft, swapBtn);
                }else if (selection == MyBaseClass.Selection.Fft){
                    selection = onSwap(MyBaseClass.Selection.ThirdOctave, swapBtn);
                }
            }
        });

        Outputs.rmsSpectrum3 = new int[noSpectralLines3];
        Outputs.maxSpectrum3 = new int[noSpectralLines3];
        Outputs.leqSpectrum3 = new int[noSpectralLines3];
        Outputs.rmsSpectrum1 = new int[noSpectralLines1];
        Outputs.maxSpectrum1 = new int[noSpectralLines1];
        Outputs.leqSpectrum1 = new int[noSpectralLines1];
        Outputs.fftSpectrum = new int[noFftSpectralLines];

        yAxis3 = (Yaxis) findViewById(R.id.Yaxis1);
        yAxis1 = (Yaxis) findViewById(R.id.Yaxis2);
        yAxisFft = (Yaxis) findViewById(R.id.Yaxis3);

        yAxis1.setVisibility(View.INVISIBLE);
        yAxisFft.setVisibility(View.INVISIBLE);

        yAxis3.Init(2000, 8000);
        yAxis1.Init(2000, 8000);
        yAxisFft.Init(1000, 8000);

        xAxis3 = (Xaxis) findViewById(R.id.Xaxis1);
        xAxis1 = (Xaxis) findViewById(R.id.Xaxis2);
        xAxisFft = (Xaxis) findViewById(R.id.Xaxis3);

        xAxis1.setVisibility(View.INVISIBLE);
        xAxisFft.setVisibility(View.INVISIBLE);

        xAxis3.Init(Xaxis.XaxisType.Cpb, 2000, 6000);
        xAxis1.Init(Xaxis.XaxisType.Cpb, 2000, 6000);
        xAxisFft.Init(Xaxis.XaxisType.Lin, 1000, 6000);

        spectrumView3 = (SpectrumView) findViewById(R.id.SpectrumView1);
        spectrumView1 = (SpectrumView) findViewById(R.id.SpectrumView2);
        fftSpectrumView = (SpectrumView) findViewById(R.id.SpectrumView3);

        spectrumView1.setVisibility(View.INVISIBLE);
        fftSpectrumView.setVisibility(View.INVISIBLE);

        spectrumView3.invalidate();
        spectrumView1.invalidate();
        fftSpectrumView.invalidate();


        //=============设置频谱===================================
//        spectrumView3.Connect(this, Outputs.rmsSpectrum3, Outputs.leqSpectrum3, noSpectralLines3, 2000, 8000);
        spectrumView3.Connect(null, null, Outputs.rmsSpectrum3, noSpectralLines3, 2000, 8000);
        spectrumView1.Connect(null, null, Outputs.rmsSpectrum1, noSpectralLines1, 2000, 8000);
//        spectrumView1.Connect(this, Outputs.rmsSpectrum1, Outputs.leqSpectrum1, noSpectralLines1, 2000, 8000);
        fftSpectrumView.Connect(null, null, Outputs.fftSpectrum, noFftSpectralLines, 1000, 8000);


//        yAxis3.addListener(spectrumView3);
//        yAxis1.addListener(spectrumView1);
//        yAxisFft.addListener(fftSpectrumView);


    }


    public void updateView(short[] buffer, String ipStr) {
        ipTv.setText("设备IP:" + ipStr);

        int count = 0;
        if (buffer != null) {
            //30 * 3 = 90
            for (int i = 0; i < noSpectralLines3; i++) {
                Outputs.rmsSpectrum3[i] = buffer[count];
                count++;
//                    Inputs.maxSpectrum3[i] = buffer[count];
                count++;
            }

            //平均值
            double total = 0.0, avg = 0.0;
            for (int i = 0; i < noSpectralLines3; i++){
                total += Math.pow(10, Outputs.rmsSpectrum3[i] / 1000.0);
            }
            avg = total / noSpectralLines3;

            for (int i = 0; i < noSpectralLines3; i++) {
//                    Inputs.leqSpectrum3[i] = buffer[count];
                count++;
            }
            //10 * 3 = 30
            for (int i = 0; i < noSpectralLines1; i++) {
                Outputs.rmsSpectrum1[i] = buffer[count];
                count++;
//                    Inputs.maxSpectrum1[i] = buffer[count];
                count++;
            }

            for (int i = 0; i < noSpectralLines1; i++) {
//                    Inputs.leqSpectrum1[i] = buffer[count];
                count++;
            }

            Outputs.rms = buffer[count];
            count++;
//                Inputs.max = buffer[count];
            count++;
            Outputs.ln1 = buffer[count];
            count++;
            Outputs.ln2 = buffer[count];
            count++;
            Outputs.ln3 = buffer[count];
            count++;
//                Inputs.leq = buffer[count];
            spTv.setText("声级：" + buffer[count] / 100.0f + " dB  " + (int)(avg / 1000) + "mV");
            count++;
//                Inputs.peak = buffer[count];
            count++;

            //==127
            for (int i = 0; i < noFftSpectralLines; i++) {
                Outputs.fftSpectrum[i] = buffer[count];
                count++;
            }
        }


        spectrumView3.invalidate();
        spectrumView1.invalidate();
        fftSpectrumView.invalidate();
    }

    public MyBaseClass.Selection onSwap(MyBaseClass.Selection selection, Button button) {
        switch (selection) {
            case ThirdOctave:
                yAxis3.setVisibility(View.VISIBLE);
                yAxis1.setVisibility(View.INVISIBLE);
                yAxisFft.setVisibility(View.INVISIBLE);
                xAxis3.setVisibility(View.VISIBLE);
                xAxis1.setVisibility(View.INVISIBLE);
                xAxisFft.setVisibility(View.INVISIBLE);
                spectrumView3.setVisibility(View.VISIBLE);
                spectrumView1.setVisibility(View.INVISIBLE);
                fftSpectrumView.setVisibility(View.INVISIBLE);
                button.setText("1/3倍频");
                return MyBaseClass.Selection.Octave;
            case Octave:
                yAxis3.setVisibility(View.INVISIBLE);
                yAxis1.setVisibility(View.VISIBLE);
                yAxisFft.setVisibility(View.INVISIBLE);
                xAxis3.setVisibility(View.INVISIBLE);
                xAxis1.setVisibility(View.VISIBLE);
                xAxisFft.setVisibility(View.INVISIBLE);
                spectrumView3.setVisibility(View.INVISIBLE);
                spectrumView1.setVisibility(View.VISIBLE);
                fftSpectrumView.setVisibility(View.INVISIBLE);
                button.setText("倍频");
                return MyBaseClass.Selection.Fft;
            case Fft:
                yAxis3.setVisibility(View.INVISIBLE);
                yAxis1.setVisibility(View.INVISIBLE);
                yAxisFft.setVisibility(View.VISIBLE);
                xAxis3.setVisibility(View.INVISIBLE);
                xAxis1.setVisibility(View.INVISIBLE);
                xAxisFft.setVisibility(View.VISIBLE);
                spectrumView3.setVisibility(View.INVISIBLE);
                spectrumView1.setVisibility(View.INVISIBLE);
                fftSpectrumView.setVisibility(View.VISIBLE);
                button.setText("FFT");
                return MyBaseClass.Selection.ThirdOctave;
        }
        return MyBaseClass.Selection.ThirdOctave;
    }


}
